package com.blockmeta.bbs.immodule.utils;

import com.blockmeta.bbs.baselibrary.utils.FormatUtil;

import java.util.Calendar;
import java.util.Date;

import static com.blockmeta.bbs.baselibrary.utils.TimeUtil.IsYesterday;

/**
 * Created by author on 2019/4/9.
 */

public class ImTimeUtils {
    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    public static String SessionListTime(int time) {
        long newTime = time * 1000L;
        Date date = new Date(newTime);
        long diff = System.currentTimeMillis() - date.getTime();
        long r = 0;
        Calendar cal = Calendar.getInstance();
        int CurrentYear = cal.get(Calendar.YEAR);

        if (diff > year || CurrentYear != FormatUtil.parseInt(FormatUtil.getTimetoHMS(newTime, "yyyy"))) {
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "yyyy/MM/dd");
        }
        if (diff > month) {
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "MM/dd");
        }


        //判断是不是昨天
        if (IsYesterday(time)) {
            return "昨天 "+FormatUtil.getTimetoHMS(newTime, "HH:mm");
        }
        if (diff > day) {//大于24小时秒值
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "MM/dd");
        }
        //今天
        return FormatUtil.getTimetoHMS(newTime, "HH:mm");
    }

    public static String lastActiveTime(int time){
        long newTime = time * 1000L;
        Date date = new Date(newTime);
        long diff = System.currentTimeMillis() - date.getTime();
        long r = 0;
        Calendar cal = Calendar.getInstance();
        int CurrentYear = cal.get(Calendar.YEAR);

        if (diff > year) {
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "yyyy/MM/dd");
        }
        if (diff > month) {
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "MM/dd");
        }


        //判断是不是昨天
        if (IsYesterday(time)) {
            return "昨天 "+FormatUtil.getTimetoHMS(newTime, "HH:mm");
        }
        if (diff > day) {//大于24小时秒值
            return FormatUtil.getTimetoHMS((int) (newTime / 1000), "MM/dd");
        }
        //今天
        return FormatUtil.getTimetoHMS(newTime, "HH:mm");
    }
}
